<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="中文简称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入中文简称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['basic:language:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:language:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:language:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="languageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="languageId" min-width="100" />
      <el-table-column label="中文简称" align="center" prop="name" min-width="100" />
      <el-table-column label="中文全称" align="center" prop="fullName" min-width="100" />
      <el-table-column label="英文简称" align="center" prop="nameEn" min-width="100" />
      <el-table-column label="英文全称" align="center" prop="fullNameEn" min-width="100" />
      <el-table-column label="简介" align="left" prop="brief" min-width="180" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="130">
        <template #default="scope">
          <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['basic:language:edit']">编辑</el-button>
          <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['basic:language:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改语言对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="languageRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="中文简称" prop="name">
          <el-input v-model="form.name" placeholder="请输入语言简称" />
        </el-form-item>
        <el-form-item label="中文全称" prop="fullName">
          <el-input v-model="form.fullName" placeholder="请输入语言全称" />
        </el-form-item>
        <el-form-item label="所属国家" prop="countryId">
          <el-select v-model="form.countryId" placeholder="请选择语言所属国家/地区" clearable style="width: 100%">
            <el-option
              v-for="item in countryOptions"
              :key="item.countryId"
              :label="item.name"
              :value="item.countryId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="英文简称" prop="nameEn">
          <el-input v-model="form.nameEn" placeholder="请输入语言英文简称" />
        </el-form-item>
        <el-form-item label="英文全称" prop="fullNameEn">
          <el-input v-model="form.fullNameEn" placeholder="请输入语言英文全称" />
        </el-form-item>
        <el-form-item label="原名简称" prop="originalName">
          <el-input v-model="form.originalName" placeholder="请输入语言原名简称" />
        </el-form-item>
        <el-form-item label="原名全称" prop="originalFullName">
          <el-input v-model="form.originalFullName" placeholder="请输入语言原名全称" />
        </el-form-item>
        <el-form-item label="简介" prop="brief">
          <el-input
            v-model="form.brief"
            type="textarea"
            :autosize="{ minRows: 2, maxRows: 5 }"
            maxlength="200"
            show-word-limit
            placeholder="语言简介"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Language">
import { listLanguage, getLanguage, delLanguage, addLanguage, updateLanguage } from "@/api/basic/language";
import { listCountry } from "@/api/basic/country";
import { getCurrentInstance, ref, reactive, toRefs } from 'vue';

const { proxy } = getCurrentInstance();

const languageList = ref([]);
const countryOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null
  },
  rules: {
    name: [
      { required: true, message: "语言简称不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询语言列表 */
function getList() {
  loading.value = true;
  listLanguage(queryParams.value).then(response => {
    languageList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询国家列表 */
function getCountryList() {
  listCountry().then(response => {
    countryOptions.value = response.rows || [];
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    languageId: null,
    name: null,
    fullName: null,
    countryId: null,
    nameEn: null,
    fullNameEn: null,
    originalName: null,
    originalFullName: null,
    brief: null
  };
  proxy.resetForm("languageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.languageId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  getCountryList();
  open.value = true;
  title.value = "添加语言";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  getCountryList();
  const _languageId = row.languageId || ids.value;
  getLanguage(_languageId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改语言";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["languageRef"].validate(valid => {
    if (valid) {
      if (form.value.languageId != null) {
        updateLanguage(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addLanguage(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const languageIds = row.languageId || ids.value;
  proxy.$modal.confirm('是否确认删除语言编号为"' + languageIds + '"的数据项？').then(function() {
    return delLanguage(languageIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>